import util from "../../utils/util"
import { getInfo } from '../../api/enterprise'
let app = getApp();
Component({
  properties: {
    distributionType: {
      type: Boolean,
      value: true,
    }
  },
  data: {
    time: ['00:00', '00:30', '01:00', '01:30', '02:00', '02:30', '03:00', '03:30', '04:00', '04:30', '05:00', '05:30', '06:30', '07:00', '07:30', '08:00', '08:30', '09:00', '09:30', '10:00', '10:30', '11:00', '11:30', '12:00',
      '12:30', '13:00', '13:30', '14:00', '14:30', '15:00', '15:30', '16:00', '16:30', '17:00', '17:30', '18:00', '18:30', '19:00', '19:30', '20:00', '20:30', '21:00', '21:30', '22:30', '23:00', '23:30'],
    otherTime: ['00:00', '00:30', '01:00', '01:30', '02:00', '02:30', '03:00', '03:30', '04:00', '04:30', '05:00', '05:30', '06:30', '07:00', '07:30', '08:00', '08:30', '09:30', '10:00', '10:30', '11:00', '11:30', '12:00',
      '12:30', '13:00', '13:30', '14:00', '14:30', '15:00', '15:30', '16:00', '16:30', '17:00', '17:30', '18:00', '18:30', '19:00', '19:30', '20:00', '20:30', '21:00', '21:30', '22:30', '23:00', '23:30'],
    date: [
      { title: '今天', date: '' },
      { title: '明天', date: '' }
    ],
    dateIdx: 0,
    timeIdx: '-1',
    currentDate: '',
    deliveryTimeType: false,
    dateForm: [],
    shippingConfig: {
      "workBeginTime": "00:15:00",
      "workEndTime": "23:59:00",
      "speed": [
        {
          "reach": ["00:00:00", "00:00:00"],
          "reachDay": 0,
          "reachTime": 15
        },
        {
          "reach": ["00:00:00", "00:00:00"],
          "reachDay": 0,
          "reachTime": 15
        }
      ],
      "shippingTime": 15,
      "shippingmake": 2
    },
    estimateDate: '',
    nowType: true,
    detaTitle: ''
  },

  attached: function () {
    this.getInfo()
  },
  methods: {
    formatNumber(num) {
      num = num.toString()
      return num[1] ? num : '0' + num
    },
    getInfo() {
      let params = {
        mchId: app.globalData.mchId,
      }
      getInfo(params).then(res => {
        this.data.shippingConfigs = JSON.parse(res.data.shippingConfig)
        const date = new Date();
        let Month = this.formatNumber(date.getMonth() + 1)
        let dataNum = this.formatNumber(date.getDate())
        let workBeginTime = date.getFullYear() + "-" + Month + "-" + dataNum + " " + this.data.shippingConfigs.workBeginTime;
        let workBeginTimeMillisecond = this.conversionTime(workBeginTime)
        let workEndTime = date.getFullYear() + "-" + Month + "-" + dataNum + " " + this.data.shippingConfigs.workEndTime;
        if (this.data.shippingConfigs.workEndTime == "00:00:00") {
          workEndTime = date.getFullYear() + "-" + Month + "-" + (dataNum + 1) + " " + this.data.shippingConfigs.workEndTime;
        }
        let workEndTimeMillisecond = this.conversionTime(workEndTime)
        let todaydeliveryTime = []
        let deliveryTime = []
        todaydeliveryTime.push('立即送出')
        this.data.time.forEach(item => {
          let timeItem = this.conversionTime(date.getFullYear() + "-" + Month + "-" + dataNum + " " + item)
          let currenttimestamp = new Date().valueOf()
          if (timeItem >= (workBeginTimeMillisecond + 3600000 * 2) && timeItem <= workEndTimeMillisecond && currenttimestamp <= timeItem) {
            todaydeliveryTime.push(item)
          }
          if (timeItem >= (workBeginTimeMillisecond + 3600000 * 2) && timeItem <= workEndTimeMillisecond) {
            deliveryTime.push(item)
          }
        })
        this.data.time = todaydeliveryTime
        this.data.otherTime = deliveryTime
        this.setData({
          time: this.data.time,
          otherTime: this.data.otherTime
        })
      })
    },
    conversionTime(data) {
      data = data.replace(" ", ":").replace(/\:/g, "-").split("-")
      let date = new Date(data[0], data[1] - 1, data[2], data[3], data[4]);
      let time = date.getTime() - 3600000 * 2;
      return time
    },
    swiperChoose(data) {
      var index = data.detail.current
      this.data.timeIdx = '-1'
      this.setData({
        dateIdx: index,
        currentDate: this.data.date[index].date,
        timeIdx: this.data.timeIdx
      })
    },
    toggleTime(e) {
      let timeIdx = e.currentTarget.dataset.index
      let currentTime = e.currentTarget.dataset.item
      let startTime = e.currentTarget.dataset.item
      this.setData({
        timeIdx: timeIdx,
        currentTime: currentTime,
        startTime: startTime
      })
      let params = {
        startTime: this.data.startTime,
        dateIdx: this.data.dateIdx
      };
      this.triggerEvent('myevent', params)
      this.deliveryTimeClick()
    },
    toggleDate(e) {
      if (this.data.dateIdx == e.currentTarget.dataset.index) {
        return false
      } else {
        this.data.timeIdx = '-1'
        this.setData({
          dateIdx: e.currentTarget.dataset.index,
          currentDate: e.currentTarget.dataset.item,
          timeIdx: this.data.timeIdx
        })
      }
    },
    deliveryTimeClick() {
      this.data.deliveryTimeType = !this.data.deliveryTimeType
      this.setData({
        deliveryTimeType: this.data.deliveryTimeType
      })
    }
  }
})
